home *** CD-ROM | disk | FTP | other *** search
/ Oh!X 2001 Spring / Oh!X 2001 Spring Special CD-ROM (Japan).7z / Oh!X 2001 Spring Special CD-ROM (Japan) (Track 1).bin / JAVA / jshooting2 / jshooting.java < prev    next >
Text File  |  2000-08-18  |  2KB  |  114 lines

  1. import java.applet.Applet;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4.  
  5. public class jshooting extends Applet implements KeyListener, Runnable {
  6.     static final int WIDTH = 240, HEIGHT = 320;    // 背景サイズ
  7.     SpriteControl sc;    // スプライトコントロール
  8.     MediaTracker mt;
  9.     boolean up=false, down=false, left=false, right=false;
  10.     int mx=(WIDTH-32)/2, my=(HEIGHT-32)/2;
  11.     Thread thread=null;
  12.     public void init(){
  13.         sc = new SpriteControl( 1, 1, WIDTH, HEIGHT, this );
  14.         mt = new MediaTracker( this );
  15.         Image image = getImage( getDocumentBase(), "img/back.gif" );    // 背景
  16.         mt.addImage( image, 0 );
  17.         sc.SetBGImage( image );
  18.         image = getImage( getDocumentBase(), "img/myship.gif" );    // 自機
  19.         mt.addImage( image, 0 );
  20.         sc.Define( 0, image );
  21.         sc.Set( 0, 0 );
  22.         sc.Move( 0, mx, my );
  23.         sc.Show();
  24.         addKeyListener( this );
  25.     }
  26.     public void update( Graphics g ){
  27.         paint( g );
  28.     }
  29.     public void paint( Graphics g ){
  30.         if( mt.checkID( 0 ) ){
  31.             sc.Display( g, this );
  32.         } else {
  33.             g.drawString( "Loading...", 0, 12 );
  34.         }
  35.     }
  36.     public void keyPressed( KeyEvent e ){
  37.         switch( e.getKeyCode() ){
  38.         case KeyEvent.VK_UP:
  39.             up = true;
  40.             break;
  41.         case KeyEvent.VK_DOWN:
  42.             down = true;
  43.             break;
  44.         case KeyEvent.VK_LEFT:
  45.             left = true;
  46.             break;
  47.         case KeyEvent.VK_RIGHT:
  48.             right = true;
  49.             break;
  50.         }
  51.     }
  52.     public void keyReleased( KeyEvent e ){
  53.         switch( e.getKeyCode() ){
  54.         case KeyEvent.VK_UP:
  55.             up = false;
  56.             break;
  57.         case KeyEvent.VK_DOWN:
  58.             down = false;
  59.             break;
  60.         case KeyEvent.VK_LEFT:
  61.             left = false;
  62.             break;
  63.         case KeyEvent.VK_RIGHT:
  64.             right = false;
  65.             break;
  66.         }
  67.     }
  68.     public void keyTyped( KeyEvent e ){
  69.     }
  70.     public void start(){
  71.         if( thread==null ){
  72.             thread = new Thread( this );
  73.             thread.start();
  74.         }
  75.     }
  76.     public void stop(){
  77.         if( thread!=null ){
  78.             thread = null;
  79.         }
  80.     }
  81.     public void run(){
  82.         try {
  83.             mt.waitForID( 0 );
  84.         } catch( InterruptedException e ){
  85.             return;
  86.         }
  87.         while( thread!=null ){
  88.             try {
  89.                 Thread.sleep( 50 );
  90.             } catch( InterruptedException e ){
  91.                 break;
  92.             }
  93.             if( up ){
  94.                 my -= 4;
  95.                 if( my<0 ) my = 0;
  96.             }
  97.             if( down ){
  98.                 my += 4;
  99.                 if( my>HEIGHT-32 ) my = HEIGHT-32;
  100.             }
  101.             if( left ){
  102.                 mx -= 4;
  103.                 if( mx<0 ) mx = 0;
  104.             }
  105.             if( right ){
  106.                 mx += 4;
  107.                 if( mx>WIDTH-32 ) mx = WIDTH-32;
  108.             }
  109.             sc.Move( 0, mx, my );
  110.             repaint();
  111.         }
  112.     }
  113. }
  114.